﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.OfficeDocument;
using Enterprise.Data.OfficeDocument;
using Enterprise.Infrastructure;
using Enterprise.Model.WorkFlow;
using Enterprise.Service.WorkFlow;
using Enterprise.Service.Rtx;
namespace Enterprise.Service.OfficeDocument
{
    /// <summary>
    /// 公文数据操作服务类
    /// </summary>
    public class OfficeDocService
    {
        /// <summary>
        /// 缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-OfficeDocument";

        /// <summary>
        /// 列表 返回所有记录
        /// </summary>
        /// <returns></returns>
        public List<OfficeDocEntity> OfficeDocumentList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<OfficeDocEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                OfficeDocData data = new OfficeDocData();
                List<OfficeDocEntity> list = data.OfficeDocmentList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 返回公文记录
        /// </summary>
        /// <param name="odId"></param>
        /// <returns></returns>
        public OfficeDocEntity OfficeDocumentDisp(string odId)
        {
            return OfficeDocumentList().Where(p => p.ODID == odId).FirstOrDefault();
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void OfficeDocumentExecute(OfficeDocEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into OFFICE_DOCUMENT(ODID,ODTYPEID,ODFROMTIME,ODFROMUSER,ODFROMCODE,ODFROM,ODCNAME,ODRNAME,ODCREMARK,ODRREMARK,ODFILES,ODSTATE,ODCHECKERS,ODDEPTID,ODUSERID,ODCREATETIME,ODISPUBLIC,ODPUBLICTIME,ODQSUSERS) values('" + entity.ODID + "'," + entity.ODTYPEID + ",'" + entity.ODFROMTIME + "','" + entity.ODFROMUSER + "','" + entity.ODFROMCODE + "','" + entity.ODFROM + "','" + entity.ODCNAME + "','" + entity.ODRNAME + "',:ODCREMARK,:ODRREMARK,'" + entity.ODFILES + "'," + entity.ODSTATE + ",'" + entity.ODCHECKERS + "'," + entity.ODDEPTID + "," + entity.ODUSERID + "," + Utility.FormatOracleDate(entity.ODCREATETIME) + "," + entity.ODISPUBLIC + "," + Utility.FormatOracleDate(entity.ODPUBLICTIME) + ",'" + entity.ODQSUSERS + "')";
                    break;
                case "Delete":
                    cmd = "Delete from OFFICE_DOCUMENT where ODID='"+entity.ODID+"'";
                    break;
                case "Public":
                    cmd = "Update OFFICE_DOCUMENT set ODTYPE='" + entity.ODTYPE + "',ODCODE='" + entity.ODCODE + "',ODCNAME='"+entity.ODCNAME+"',ODRNAME='"+entity.ODRNAME+"',ODCREMARK=:ODCREMARK,ODRREMARK=:ODRREMARK,ODFILES='"+entity.ODFILES+"',ODISPUBLIC="+entity.ODISPUBLIC+",ODPUBLICTIME="+Utility.FormatOracleDate(entity.ODPUBLICTIME)+",ODQSUSERS='" + entity.ODQSUSERS + "' where ODID='" + entity.ODID+"'";
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                if (entity.DB_Option_Action == "Delete")
                {
                    OfficeDocumentExecuteBySql(cmd,1);
                }
                else
                {
                    OfficeDocumentExecuteBySqlCommand(cmd, 1,"ODCREMARK", entity.ODCREMARK, "ODRREMARK", entity.ODRREMARK);
                }
            }
        }

        /// <summary>
        /// 操作方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1:刷新缓存 0:不刷新缓存 </param>
        public void OfficeDocumentExecuteBySql(string cmd, int typeId)
        {
            OfficeDocData data = new OfficeDocData();
            data.OfficeDocumentExecute(cmd);
            if (typeId == 1)
            {
                ClearCache();
            }
        }

        /// <summary>
        /// 通知公告操作
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void OfficeDocumentExecuteBySqlCommand(string cmd,int typeId, string item1, object item1Value, string item2, object item2Value)
        {
            OfficeDocData data = new OfficeDocData();
            data.OfficeDocumentExecute(cmd, item1, item1Value, item2, item2Value);
            if (typeId == 1)
            {
                ClearCache();
            }
        }

        /// <summary>
        ///  按签收范围进行筛选可查看的通知公告
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<OfficeDocEntity> OfficeDocQianshouList(List<OfficeDocEntity> list)
        {
            //加入查看范围控制
            //签收范围以及发布者则可查看
            OfficeRecevieService orService = new OfficeRecevieService();
            List<OfficeDocEntity> reList = new List<OfficeDocEntity>();
            foreach (OfficeDocEntity model in list)
            {
                bool rbool = orService.OfficeQianshouCheck(model.ODQSUSERS);
                if (rbool)
                {
                    reList.Add(model);
                }
            }
            return reList;
        }

        /// <summary>
        /// 返回所有待用户审核的公文申请单
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public List<WorkFlowProcessEntity> OfficeDocumentUnCheck(int UserId)
        {
            WorkFlowProcessService wfService = new WorkFlowProcessService();
            return wfService.WorkFlowProcessList().Where(p => p.PISCHECK == 0 && p.PUSERID == UserId && p.PWFTYPE == 1).ToList();
        }

        /// <summary>
        /// 操作方法语句
        /// </summary>
        /// <param name="iIndex"></param>
        /// <param name="ho"></param>
        public void Notice(OfficeDocEntity ho, int iIndex)
        {
            string[] s = ho.ODCHECKERS.Split(',');
            if (s.ToList().Count > 0)
            {
                if (s.ToList().Count > iIndex)
                {
                    int checkUserId = Convert.ToInt32(s.ToList()[iIndex]);
                    int userId = checkUserId;
                    //判断是否存在授权设置
                    UserWfConfigService cfService = new UserWfConfigService();
                    var query = cfService.UserWfConfig(Utility.Get_UserId).Where(p => p.WFTYPEID == 1 && p.ENDTIME > DateTime.Now).FirstOrDefault();
                    if (query != null&&query.SQUSERID!=0)
                    {
                        userId = query.SQUSERID;
                    }
                    WorkFlowProcessService wfpService = new WorkFlowProcessService();
                    wfpService.WorkFlowProcessInsertNew(ho.ODID, checkUserId, userId, 1, iIndex + 1);
                    #region "发送Rtx消息"
                    string sUserName = Enterprise.Service.User.UserService.GetUserName(ho.ODUSERID);
                    string sTitle = string.Format("审批流程[公文审批申请(员工:{0})]", sUserName);
                    string sContent = sTitle + ",请登录平台完成公文审批";
                    RtxService.SendRtxMessageServices(userId, sTitle, sContent);
                    #endregion
                }
                else if (s.ToList().Count == iIndex)
                {
                    ho.ODSTATE = 1;
                    string cmd = "Update OFFICE_DOCUMENT set ODSTATE=" + ho.ODSTATE + " where ODID='" + ho.ODID + "'";
                    OfficeDocumentExecuteBySql(cmd, 1);
                    string sTitle = string.Format("审批流程[您的公文({0})(员工:{0})]", ho.ODCNAME);
                    string sContent = sTitle + string.Format(",已经通过审批流程!");
                    RtxService.SendRtxMessageServices(ho.ODUSERID, sTitle, sContent);
                }
            }
        }
    }
}
